package com.nhjf.admin.controller.system;

import com.alibaba.fastjson.JSON;
import com.nhjf.admin.controller.BaseController;
import com.nhjf.admin.util.Enums;
import com.nhjf.admin.util.Jurisdiction;
import com.nhjf.command.system.CreateResourceCommand;
import com.nhjf.command.system.ModifyResourceCommand;
import com.nhjf.model.system.Resource;
import com.nhjf.model.system.User;
import com.nhjf.model.ztreeModel.ZtreeModel;
import com.nhjf.pojo.dto.result.ServiceResultDto;
import com.nhjf.pojo.qo.system.ResourceQo;
import com.nhjf.service.system.ResourceService;

import net.sf.json.JSONArray;

import org.apache.shiro.session.InvalidSessionException;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @类功能说明：资源管理
 * @部门：技术部
 * @作者：龚一凡
 * @版本：V1.0
 */
@Controller
@RequestMapping(value = "/resource")
public class ResourceController extends BaseController {
    @Autowired
    private ResourceService resourceService;

    /**
     *
     * @param request
     * @param model
     * @return
     */
    public String sysRole(HttpServletRequest request, Model model){
        return null;
    }
    
	/**
	     * 
	     * @方法功能说明：资源树
	     * @修改者名字：龚一凡
	     * @修改内容：
	     * @参数：
	     * @return:
	     * @throws
	     */
	@RequestMapping(value= "/resource_tree")
	public String resource(HttpServletRequest request,Model model) {
    	ResourceQo resourceQo=new ResourceQo();
    	List<Resource>	reslist=resourceService.queryList(resourceQo);//
    	List<ZtreeModel> ztreelist=new ArrayList<ZtreeModel>();
    	int i=0;
    	for(Resource res:reslist) {

        		ZtreeModel ztree=new ZtreeModel();
        		ztree.setId(res.getId().intValue());
        		ztree.setName(res.getName());
        		ztree.setpId(res.getParentId().intValue());
    			if(res.getId()==281L) {
    				ztree.setOpen(true);
    			}
        		ztreelist.add(ztree);
//        		System.out.println(res.getName());

    	}
    	System.out.println(JSONArray.fromObject(ztreelist).toString());
    	model.addAttribute("ztreelist",JSONArray.fromObject(ztreelist).toString());
		return "/system/resource_tree.html";
	}
	/**
     * 
     * @方法功能说明：去编辑页面
     * @修改者名字：龚一凡
     * @修改内容：
     * @参数：
     * @return:
     * @throws
     */
@RequestMapping(value= "/resource_toedit")
public String toEdit(HttpServletRequest request,Model model) {
	String id=request.getParameter("id").toString();
	Resource res=resourceService.get(Long.parseLong(id));
	model.addAttribute("resource",res);
	return "/system/resource_edit.html";
	}
/**
 * 
 * @方法功能说明：去新增页面 
 * @修改者名字：龚一凡
 * @修改内容：
 * @参数：
 * @return:
 * @throws
 */
@RequestMapping(value= "/resource_toadd")
public String toAdd(HttpServletRequest request,Model model) {
	String id=request.getParameter("id").toString();
	Resource res=new Resource();
	res.setParentId(Long.parseLong(id));
	model.addAttribute("resource",res);
	return "/system/resource_add.html";
}
/**
     * 
     * @方法功能说明：新建资源保存
     * @修改者名字：龚一凡
     * @修改内容：
     * @参数：
     * @return:
     * @throws
     */
@ResponseBody
@RequestMapping(value= "/resource_save")
public String save(HttpServletRequest request,HttpServletResponse response,@ModelAttribute CreateResourceCommand resource,Model model) {
	try {
		User user=(User)Jurisdiction.getSession().getAttribute(com.nhjf.common.Constants.USER_INFO);
		if ( user== null) {
			// 跳转到登录页面
			response.sendRedirect(request.getContextPath()+"/login");
		}
		resource.setUser((User)Jurisdiction.getSession().getAttribute(com.nhjf.common.Constants.USER_INFO));
		ServiceResultDto serviceResultDto=resourceService.createResource(resource);
		if(serviceResultDto.getSuccess()) {
			return "200";
		}else {
			return "500";
		}
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return "500";
	}
/**
 * 
 * @方法功能说明：资源更新
 * @修改者名字：龚一凡
 * @修改时间：2017年8月22日上午9:45:54
 * @修改内容：
 * @参数：
 * @return:
 * @throws
 */
@ResponseBody
@RequestMapping(value= "/resource_update")
public String update(HttpServletRequest request,HttpServletResponse response,@ModelAttribute ModifyResourceCommand modifyResourceCommand,Model model) {

	
	ServiceResultDto serviceResultDto;
	try {
		User user=(User)Jurisdiction.getSession().getAttribute(com.nhjf.common.Constants.USER_INFO);
		if ( user== null) {
			// 跳转到登录页面
			response.sendRedirect(request.getContextPath()+"/login");
		}
		serviceResultDto = resourceService.modifyResource(modifyResourceCommand);
		if(serviceResultDto.getSuccess()) {
			return "200";
		}else {
			return "500";
		}
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return "500";
	
		
	}
/**
 * 
 * @方法功能说明：资源删除
 * @修改者名字：龚一凡
 * @修改时间：2017年8月14日上午10:05:30
 * @修改内容：
 * @参数：
 * @return:
 * @throws
 */
@ResponseBody
@RequestMapping(value= "/resource_delete")
public String delete(HttpServletRequest request,HttpServletResponse response,String resourceId) {
		try {
			User user=(User)Jurisdiction.getSession().getAttribute(com.nhjf.common.Constants.USER_INFO);
			if ( user== null) {
				// 跳转到登录页面
				response.sendRedirect(request.getContextPath()+"/login");
			}
			ResourceQo qo=new ResourceQo();
			qo.setParentId(Long.parseLong(resourceId));
			Integer num=resourceService.queryCount(qo);
			if(num>0) {
				return "500";
			}
				resourceService.deleteById(Long.parseLong(resourceId));

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "500";
		}
			return "200";
	}
}
